﻿CREATE FUNCTION [dbo].[GetSupplierPrice]
(
@PartyId int
)
RETURNS DECIMAL(6,2)
AS
BEGIN
DECLARE @DocTypeId int
DECLARE @InputLineId int
DECLARE @GoodsId int
DECLARE @SupplierPrice decimal
DECLARE @RetailPack int
DECLARE @Count int
select @DocTypeId = d.DocTypeId from
CA.Documents d inner join CD.Parties p on
p.DocId = d.Id and p.Id = @PartyId
if (@DocTypeId <> 12)
BEGIN
RETURN 0
END
select top 1 @InputLineId = InputLineId,@GoodsId = GoodsId  from CD.Parties
where id = @PartyId
order by RetailPrice
select @SupplierPrice = SupplierPrice from CD.InputLines
where id = @InputLineId
select @RetailPack = RetailPack from CA.Goods
where ChildId = @GoodsId
select @Count = RetailPack from CA.Goods
where id = @GoodsId
set @Count = @RetailPack/@Count
return @SupplierPrice/cast(@Count as decimal)
END

